/**
 * 通用js方法封装处理
 * Copyright (c) 2019 yiyue
 */
import moment from "moment";


// 日期格式化  IE9,10,11不兼容时间的处理，使用js moment插件兼容
export function dateFormat(date, pattern) {
  //var d = new Date(date).Format("yyyy-MM-dd hh:mm:ss");
  //if (pattern) {
  //d = new Date(date).Format(pattern);
  //}
  if (!date) {
    return;
  }
  if (pattern) {
    return moment(date).format(pattern);
  }
  return moment(date).format('YYYY-MM-DD HH:mm:ss');
}

// 表单重置
export function resetForm(refName) {
  if (this.$refs[refName] !== undefined) {
    this.$refs[refName].resetFields();
  }
}

// 添加日期范围
export function addDateRange(params, dateRange) {
  var search = params;
  if (null != dateRange) {
    search.params = {
      beginTime: this.dateRange[0],
      endTime: this.dateRange[1]
    };
  } else {
    search.params = {
      beginTime: null,
      endTime: null
    };
  }
  return search;
}

export function queryDateRange(params, dateRange) {
  if (dateRange) {
    params.startTime = dateRange[0] + " 00:00:00";
    params.endTime = dateRange[1] + " 23:59:59";
  } else {
    params.startTime = null;
    params.endTime = null;
  }
  return params;
}

// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).map((key) => {
    if (datas[key].dictValue == ('' + value)) {
      actions.push(datas[key].dictLabel);
      return false;
    }
  });
  return actions.join('');
}

// 字符串格式化(%s )
export function sprintf(str) {
  var args = arguments, flag = true, i = 1;
  str = str.replace(/%s/g, function () {
    var arg = args[i++];
    if (typeof arg === 'undefined') {
      flag = false;
      return '';
    }
    return arg;
  });
  return flag ? str : '';
}

//金额格式化 //默认2位
export function formatCurrency(num, n = 2) {
  if (!num) {
    return "0";
  }
  num = String(num.toFixed(n));
  let re = /(-?\d+)(\d{3})/;

  while (re.test(num)) {
    num = num.replace(re, "$1,$2");

  }
  return num;
}

Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                   // 月份
    "d+": this.getDate(),                        // 日
    "h+": this.getHours(),                       // 小时
    "m+": this.getMinutes(),                     // 分
    "s+": this.getSeconds(),                     // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    "S": this.getMilliseconds()                  // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

String.prototype.format = function () {
  if (arguments.length === 0) {
    return this;
  }
  for (var s = this, i = 0; i < arguments.length; i++) {
    s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
  }
  return s;
};
